﻿using System;
using System.Drawing;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LowLevelGraphics.Filter
{
    /// <summary>
    /// Symmetric nearest neighbour is a filter that is similar to the kuwahara filter
    /// </summary>
    public class SymmetricNearestNeighbour : BaseImageFilter
    {
        protected int m_nSize = 3;

        /// <summary>
        /// Initializes a new instance of the <see cref="SymmetricNearestNeighbour"/> class.
        /// </summary>
        public SymmetricNearestNeighbour()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="SymmetricNearestNeighbour"/> class.
        /// </summary>
        public SymmetricNearestNeighbour(int _nSize)
        {
            m_nSize = _nSize;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="SymmetricNearestNeighbour"/> class.
        /// </summary>
        /// <param name="_symmetricNearestNeighbour">The _symmetric nearest neighbour.</param>
        internal SymmetricNearestNeighbour(SymmetricNearestNeighbour _symmetricNearestNeighbour)
        {
        }

        /// <summary>
        /// Gets or sets the size.
        /// </summary>
        /// <value>The size.</value>
        public int Size
        {
            get { return m_nSize; }
            set { m_nSize = value; }
        }

        /// <summary>
        /// Execute this Filter
        /// A Debug Statement is written if a concrete Filter is called
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            UnsafeBitmap TempBitmap = (UnsafeBitmap)_bitmap.Clone();
            UnsafeBitmap NewBitmap = _bitmap;

            int nHeight = _bitmap.Width;
            int nWidth = _bitmap.Height;

            Random TempRandom = new Random();
            int ApertureMinX = -(Size / 2);
            int ApertureMaxX = (Size / 2);
            int ApertureMinY = -(Size / 2);
            int ApertureMaxY = (Size / 2);

            for (int x = 0; x < nWidth; ++x)
            {
                for (int y = 0; y < nHeight; ++y)
                {
                    int RValue = 0;
                    int GValue = 0;
                    int BValue = 0;
                    int NumPixels = 0;
                    for (int x2 = ApertureMinX; x2 < ApertureMaxX; ++x2)
                    {
                        int TempX1 = x + x2;
                        int TempX2 = x - x2;
                        if (TempX1 >= 0 && TempX1 < NewBitmap.Width && TempX2 >= 0 && TempX2 < NewBitmap.Width)
                        {
                            for (int y2 = ApertureMinY; y2 < ApertureMaxY; ++y2)
                            {
                                int TempY1 = y + y2;
                                int TempY2 = y - y2;
                                if (TempY1 >= 0 && TempY1 < NewBitmap.Height && TempY2 >= 0 && TempY2 < NewBitmap.Height)
                                {
                                    Color TempColor = TempBitmap.GetPixel(x, y);
                                    Color TempColor2 = TempBitmap.GetPixel(TempX1, TempY1);
                                    Color TempColor3 = TempBitmap.GetPixel(TempX2, TempY2);
                                    if (ColorFunctions.DistanceSquared(TempColor, TempColor2) <
                                        ColorFunctions.DistanceSquared(TempColor, TempColor3))
                                    {
                                        RValue += TempColor2.R;
                                        GValue += TempColor2.G;
                                        BValue += TempColor2.B;
                                    }
                                    else
                                    {
                                        RValue += TempColor3.R;
                                        GValue += TempColor3.G;
                                        BValue += TempColor3.B;
                                    }
                                    ++NumPixels;
                                }
                            }
                        }
                    }
                    Color MeanPixel = Color.FromArgb(RValue / NumPixels,
                        GValue / NumPixels,
                        BValue / NumPixels);
                    NewBitmap.SetPixel(x, y, MeanPixel);
                }
            }

            return NewBitmap;
        }

        /// <summary>
        /// This is used to clone a filter, each filter must implement it's own clone functionality
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new SymmetricNearestNeighbour(this);
        }
    }
}
